package com.onlineMIS.ORM.DAO.headQ.inventoryFlow;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.entity.chainS.inventoryFlow.ChainInventoryDTO;
import com.onlineMIS.ORM.entity.headQ.inventory.InventoryOrderProduct;
import com.onlineMIS.ORM.entity.headQ.inventoryFlow.HeadQInventoryStock;
import com.onlineMIS.ORM.entity.headQ.inventoryFlow.HeadqInventoryDTO;
import com.onlineMIS.ORM.entity.headQ.inventoryFlow.HeadqInventoryFlowOrderProduct;
import com.onlineMIS.common.Common_util;
import com.sun.istack.internal.FinalArrayList;


@Repository
public class HeadQInventoryFlowOrderProductDAOImpl extends BaseDAO<HeadqInventoryFlowOrderProduct> {

	@SuppressWarnings("unchecked")
	public int deleteProducts(final int orderId,final Set<Integer> productIds) {
		String params = "-1";
		for (Integer productId: productIds) {
			params += ","+productId;
		}
		
		final String hql = "delete from HeadqInventoryFlowOrderProduct  where flowOrder.id ="+orderId+" and productBarcode.id in("+ params +")" ;

		return executeHQLUpdateDelete(hql, null, true);

	}

		/**
	 * 根据单据id集合查询货品信息
	 * @param orderIds
	 * @return
	 */
	public List<HeadqInventoryDTO> selectByOrderIds(List<Integer> orderIds) {
		List<HeadqInventoryDTO> list = new ArrayList<HeadqInventoryDTO>();
		String hql = "select productBarcode.id, sum(quantity) from HeadqInventoryFlowOrderProduct where flowOrder.id in (";
		if (orderIds.size() != 0) {
			for (int i = 0; i < orderIds.size(); i++) {
				if (i == orderIds.size() - 1) {
					hql += orderIds.get(i) + ") group by productBarcode.id";
				} else {
					hql += orderIds.get(i) + ",";
				}
			}
		}
		List<Object> values = this.executeHQLSelect(hql, null, null, true);
		if (values.size() != 0) {
			for (Object record : values) {
				HeadqInventoryDTO dto = new HeadqInventoryDTO();
				Object[] recordResult = (Object[]) record;
				dto.setProductId(Common_util.getInt(recordResult[0]));
				dto.setSumQuantity(Common_util.getInt(recordResult[1]));
				list.add(dto);
			}
		}
		return list;
	}
	
	
	public List<HeadqInventoryFlowOrderProduct> selectProductsByOrderId(int orderId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadqInventoryFlowOrderProduct.class);
		
		criteria.add(Restrictions.eq("flowOrder.id", orderId));
		criteria.addOrder(Order.desc("productBarcode.id"));
		List<HeadqInventoryFlowOrderProduct> list = this.getByCritera(criteria, true); 
		return list;
	}
	
	public HeadqInventoryFlowOrderProduct selectProductsByOrderIdAndPbId(int orderId,int pbId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(HeadqInventoryFlowOrderProduct.class);
		
		criteria.add(Restrictions.eq("flowOrder.id", orderId));
		criteria.add(Restrictions.eq("productBarcode.id", pbId));
		List<HeadqInventoryFlowOrderProduct> list = this.getByCritera(criteria, true); 
		if (list.size() !=0) {
			return list.get(0);
		}else {
			return null;
		}
	}


}
